package cn.house.controller.admin;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.house.common.EntityConstants;
import cn.house.common.LayuiTableResult;
import cn.house.common.Pagination;
import cn.house.common.PublicResultJosn;
import cn.house.controller.BaseController;
import cn.house.entity.ApplySite;
import cn.house.service.ApplySiteService;

/**
 * <p>
 * 公租房申请地点表 前端控制器
 * </p>
 *
 * @author yuyong
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/apply-site")
public class ApplySiteController extends BaseController {

	@Autowired
	private ApplySiteService applySiteService;

	/**
	 * 增加公租房申请点
	 */
	@PostMapping
	public PublicResultJosn add(@RequestBody ApplySite applySite) {
		applySite.setStatus(EntityConstants.DATA_YES);
		applySiteService.save(applySite);
		return new PublicResultJosn();
	}

	/**
	 * 删除公租房申请点（禁用）
	 */
	@DeleteMapping
	public PublicResultJosn delete(@RequestBody List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			throw new IllegalArgumentException("不合法参数");
		}
		List<ApplySite> entityList = ids.parallelStream().map(id -> {
			ApplySite applySite = new ApplySite();
			applySite.setId(id);
			applySite.setStatus(0);
			return applySite;
		}).collect(Collectors.toList());
		applySiteService.updateBatchById(entityList);
		return new PublicResultJosn();
	}

	/**
	 * 根据id更新数据
	 */
	@PutMapping
	public PublicResultJosn updateById(@RequestBody ApplySite entity) {
		if (null == entity.getId()) {
			throw new IllegalArgumentException("不合法参数");
		}
		applySiteService.updateById(entity);
		return new PublicResultJosn();
	}

	/**
	 * 根据id查询数据
	 */
	@GetMapping("/{id}")
	public PublicResultJosn selectById(@PathVariable(value = "id", required = true) Integer id) {
		ApplySite applySite = applySiteService.getById(id);
		return new PublicResultJosn(applySite);
	}

	/**
	 * 分页模糊查询
	 */
	@GetMapping
	public LayuiTableResult selectByPage(Pagination pagination, ApplySite applySite) {
		applySite.setStatus(EntityConstants.DATA_YES);
		Wrapper<ApplySite> queryWrapper = new QueryWrapper<>(applySite);
		Page<ApplySite> page = new Page<>(pagination.getPage(), pagination.getPageSize());
		IPage<ApplySite> pageResult = applySiteService.page(page, queryWrapper);
		LayuiTableResult result = new LayuiTableResult(pageResult.getTotal(), pageResult.getRecords());
		return result;
	}

}
